package com.bingfor.easycablelink.product;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ProgressBar;

import com.bingfor.easycablelink.R;
import com.bingfor.easycablelink.base.BaseActivity;
import com.bingfor.easycablelink.base.WebViewActivity;
import com.bingfor.easycablelink.databinding.ActivityProductDetailsBinding;
import com.bingfor.easycablelink.my.ShoppingCartActivity;
import com.bingfor.easycablelink.product.viewModel.ProductDetailViewModel;
import com.bingfor.easycablelink.widgets.ServiceDialog;
import com.bingfor.easycablelink.widgets.ShareDialog;

public class ProductDetailsActivity extends BaseActivity {

    ActivityProductDetailsBinding binding;
    private ProgressBar mProgressBar;
    private WebView mWebView;
    private String url = "http://www.baidu.com";
    private String title = "详情";
    private String flag = "0" ;
    boolean showInfor = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityProductDetailsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initToolbar();
    }

    @Override
    public void initViews() {
        binding.setViewModel(new ProductDetailViewModel(mContext));
        mProgressBar = $(R.id.mProgressBar);
        mProgressBar.setMax(100);
        mWebView = $(R.id.mWebView);
        WebSettings settings = mWebView.getSettings();
        settings.setSupportZoom(false);
        settings.setDisplayZoomControls(false);
        settings.setBuiltInZoomControls(false);
        try {
            //WebView启用Javascript脚本执行
            settings.setJavaScriptEnabled(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            settings.setJavaScriptCanOpenWindowsAutomatically(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        settings.setAppCacheEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setAllowFileAccess(true);
        settings.setDomStorageEnabled(true);
        mWebView.setWebChromeClient(new ProductDetailsActivity.MyWebChromeClient());
        mWebView.setWebViewClient(new ProductDetailsActivity.MyWebViewClient());
        mWebView.loadUrl(url);

    }

    @Override
    public void initToolbar() {
        Toolbar toolbar = $(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setTitle("产品详情介绍");
        initViews();
    }

    @Override
    public void refresh() {

    }

    public class MyWebChromeClient extends android.webkit.WebChromeClient {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
//            if (waitDialog != null) {
//                if (newProgress == 100) {
//                    if (waitDialog.isShowing())
//                        waitDialog.dismiss();
//                } else {
//                    if (!waitDialog.isShowing()) {
//                        waitDialog.setMessage("正在加载 ...");
//                        waitDialog.show();
//                    }
//                }
//            }
            mProgressBar.setProgress(newProgress);
            super.onProgressChanged(view, newProgress);
        }

        @Override
        public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
            new AlertDialog.Builder(mContext).setMessage(message).setPositiveButton("确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    result.confirm();
                }
            }).setCancelable(false).show();

            return true;
        }

        @Override
        public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
            new AlertDialog.Builder(mContext).setMessage(message).setPositiveButton("确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    result.confirm();
                }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    result.cancel();
                }
            }).show();
            return true;
        }

        @Override
        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());

            builder.setMessage(message);

            final EditText et = new EditText(view.getContext());
            et.setSingleLine();
            et.setText(defaultValue);
            builder.setView(et)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            result.confirm(et.getText().toString());
                        }

                    })
                    .setNeutralButton("取消", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            result.cancel();
                        }
                    });
            builder.setCancelable(false);
            AlertDialog dialog = builder.create();
            dialog.show();
            return true;

        }

    }

    private class MyWebViewClient extends WebViewClient {


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mProgressBar.setVisibility(View.GONE);
//            if (url.contains("showTechnique")) {
//                setTitle("考试技巧");
//            } else
//                setTitle(view.getTitle());
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            mProgressBar.setVisibility(View.VISIBLE);
        }
    }
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK)
            if (mWebView.canGoBack()) {
                mWebView.goBack();
            } else onBackPressed();
        return true;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_right_product_control, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_call:
                moveToNextPage(ServiceDialog.class);
                break;
            case R.id.menu_cat:
                moveToNextPage(ShoppingCartActivity.class);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
